package main.generator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLStreamException;

import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JMethod;

public class ClassSourceCodeGenerator {

  public static void generate(ClassTranslation classTranslation) throws JClassAlreadyExistsException, IOException {
    List<Translation> packages = classTranslation.getPackages();
    String packagePath = "";
    for(int i = 0; i< packages.size(); i++) {
      packagePath += packages.get(i).getCn();
      if (i!= packages.size() -1) {
        packagePath += ".";
      }
    }
    JCodeModel cm = new JCodeModel();
    JDefinedClass dc = cm._class(packagePath);
    
    Map<String, String> methods = classTranslation.getMethods();
    for(String methodEn : methods.keySet()) {
      // TODO: 创建method，示例如下：
      /*JMethod m = dc.method(0, int.class, "foo");
      m.body()._return(JExpr.lit(5));*/
    }

    File dir2 = new File(".");
    File file = new File(dir2.getAbsolutePath() + "/src");
    file.mkdirs();
    cm.build(file);
  }
  
  public static void main(String[] args) {
    List<ClassTranslation> classes;
    try {
      classes = ClassTranslationParser.parse("translationArrayList.xml");
      for(ClassTranslation classTranslation : classes) {
        generate(classTranslation);
      }
    } catch (FileNotFoundException | XMLStreamException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JClassAlreadyExistsException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
